<template>
  <div class="wrap">
    <div class="header">
      <div class="left">

      <div class="right">

      </div>
    </div>

    </div>
    <div class="content">
      <el-table
        v-loading="loading"
        :data="data"
        border
      >
        <el-table-column
          prop="showName"
          :label="$t('table.未绑定店铺')"
          align="center"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          prop="userAccount"
          :label="$t('table.所属用户')"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="role"
          :label="$t('table.业务')"
          align="center"
          min-width="100"
        >
          <template v-slot="{row}">
            {{getRoleName(row.role)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="adminAccount"
          :label="$t('table.原属运营账号')"
          align="center"
          min-width="100"
        >
          <template v-slot="{row}">
            {{row.adminAccount || '-'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :label="$t('table.取消绑定时间')"
          align="center"
          min-width="120"
        >
          <template v-slot="{row}">
            {{row.createTime || '-'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="newAdminId"
          :label="$t('table.绑定新运营账号')"
          align="center"
          min-width="150"
        >
          <template v-slot="{row}">
            <el-select v-model="row.newAdminId" :placeholder="$t('common.请选择')" @change="changeAdminId(row)">
              <el-option
                v-for="item in (row.role === ROLE_ONE_SHIPPING ? oneShippingAdminList : middleTransferAdminList)"
                :key="item.adminId"
                :label="item.account"
                :value="item.adminId"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>

      </el-table>
    </div>

  </div>
</template>

<script>
import {deepCopy, generateRandomString} from "@/utils";
import request from "@/utils/request";
import userAccountOptions from "@/views/order/mixin/userAccountOptions";
import {editAdmin, getAdminList, getAllAdmin, getAllAuthAccount, saveAdmin} from "@/api/admin";
import {getWarehouseList} from "@/api/warehouse";
import user from "@/store/modules/user";
import {
  getRoleName, getRoleTypeName,
  ROLE_DATA_MANAGE,
  ROLE_FINANCE,
  ROLE_FINANCE_DATA,
  ROLE_MIDDLE_TRANSFER,
  ROLE_ONE_SHIPPING,
  ROLE_OPERATE, ROLE_REMITTANCE_MANAGE, ROLE_USER_MANAGE
} from "@/utils/admin";
import {MessageBox} from "element-ui";
import {bindAccount, getUnbindAccountList} from "@/api/account";
import {allocBuffer} from "jszip/lib/nodejsUtils";



export default {
  name: "unBindAccountList",
  mixins: [userAccountOptions],
  computed: {
    oneShippingAdminList(){
      return this.adminList.filter(item => item.roles.indexOf(ROLE_ONE_SHIPPING) !== -1)
    },
    middleTransferAdminList(){
      return this.adminList.filter(item => item.roles.indexOf(ROLE_MIDDLE_TRANSFER) !== -1)
    },
  },
  data(){
    return {
      loading: false,
      ROLE_ONE_SHIPPING: ROLE_ONE_SHIPPING,
      data: [],
      adminList: [],
    }
  },
  created() {
    this.getData()
    this.getAllAdmin()
  },
  methods:{
    allocBuffer,
    getRoleName,
    getData(){
      this.loading = true
      getUnbindAccountList(this.queryParams)
        .then(res=>{
          this.loading = false
          this.data = res.data.map(item=>{
            item.newAdminId = ''
            return item
          })
        })
    },
    getAllAdmin(){
      getAllAdmin()
        .then(res=>{
          this.adminList = res.data
        })
    },
    changeAdminId(row){
      let adminAccount = this.adminList.find(item=> item.adminId === row.newAdminId).account
      let message = this.$t('是否将店铺 [店铺名店铺名] 的 [业务名]业务 绑定在 [运营账号名] 下？',{accountName: row.showName, role: getRoleName(row.role), adminAccount: adminAccount})
      this.$confirm(message, '绑定新运营账号？',{})
        .then(()=>{
          this.bindAccount(row)
        })
        .catch(()=>{
          row.newAdminId = ''
        })
    },
    bindAccount(row){
      const postData = {
        ogAccountId: row.ogAccountId,
        role: row.role,
        adminId: row.newAdminId,
        userId: row.userId
      }
      bindAccount(postData)
        .then(res=>{
          if(res.error === 0){
            this.getData()
            this.$store.dispatch('user/getInfo');
            this.$successMessage(res.message)
          }else{
            this.$errorMessage(res.message)
          }
        })
    }
  }
}
</script>

<style>
.admin-dialog{
  .el-tabs__item{
    height: 40px;
    line-height: 32px;
  }
}

</style>

<style scoped lang="scss">
.wrap {
  padding: 30px 30px;
  .header{
    display: flex;
    justify-content: space-between;
  }
  .content{
    padding: 20px 0 0;
  }
}
.header-select{
  margin-right: 20px;
}
.admin-dialog{
  .account-auth-content{
    display: flex;
    .user-account{
      min-width: 100px;
    }
    .account-list{
      .account-checkbox{
        width: 120px;
      }
    }
  }
}
</style>
